package odeoncinemaassignment;
import java.sql.*;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Locale;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
public class retailFrame extends javax.swing.JFrame {
    Connection conn=utilities.connectDB(); // open database connection
    ResultSet rs = null;
    PreparedStatement pst = null;    
    String time;
    public static ArrayList al = new ArrayList();
    public DefaultListModel lm = new DefaultListModel();//Static list model to store titles populated by db hence public
    DecimalFormat df = new DecimalFormat("##.##");
    public static final String DATE_FORMAT_NOW = "dd-MMM-yyyy hh:mm"; // stores the date format. 
    static String salesReportOutput="";
    static double runningTotal=0;
    //String filmTitle;
    String recieptOutput;
    java.util.Date d = new java.util.Date();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); // To format date so is entered into database correctly
    

    String date = sdf.format(d); //apply date format to date object for correct insertion to db
    
    
    public retailFrame() throws SQLException {
        initComponents();
        utilities.populateJList(filmList, "select* from film", conn);  
        // get resultset to create arraylist of movie objects
        ResultSet mrs = utilities.getResultSet("select * from film", conn);
        
        while(mrs.next()){
            Movie m = new Movie();
            Object movie;
            int rowNum = mrs.getRow();
            m.setTitle(mrs.getString("title")); // sets title of current movie object
            m.setStarring(mrs.getString("starring"));
            m.setSynopsis(mrs.getString("synopsis"));
            m.setDirector(mrs.getString("director"));
            m.setDuration(mrs.getString("duration"));
            
            al.add(m);
            System.out.print(m.getTitle()+"     ");
        }
        
        
        
    }
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        timeButtonGroup = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        filmList = new javax.swing.JList();
        retailFrameDate = new javax.swing.JLabel();
        extrasTab = new javax.swing.JTabbedPane();
        jPanel3 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        salesSystemFilmTitle = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        starringTextField = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        synopsisTextArea = new javax.swing.JTextArea();
        jLabel4 = new javax.swing.JLabel();
        directorTextField = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        durationTextField = new javax.swing.JTextField();
        selectMovieBtn = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Select Film", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial Black", 1, 12))); // NOI18N

        filmList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                filmListMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(filmList);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        retailFrameDate.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        retailFrameDate.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        retailFrameDate.setPreferredSize(new java.awt.Dimension(34, 14));

        extrasTab.setBackground(new java.awt.Color(255, 255, 255));

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));

        jLabel1.setText("Film Chosen: ");

        salesSystemFilmTitle.setFont(new java.awt.Font("Tahoma", 1, 20)); // NOI18N
        salesSystemFilmTitle.setHorizontalAlignment(javax.swing.JTextField.CENTER);

        jLabel2.setText("Starring");

        starringTextField.setFont(new java.awt.Font("Tahoma", 1, 20)); // NOI18N
        starringTextField.setHorizontalAlignment(javax.swing.JTextField.CENTER);

        jLabel3.setText("Synopsis");

        synopsisTextArea.setColumns(20);
        synopsisTextArea.setFont(new java.awt.Font("Tahoma", 1, 13)); // NOI18N
        synopsisTextArea.setRows(5);
        jScrollPane2.setViewportView(synopsisTextArea);

        jLabel4.setText("Director");

        directorTextField.setFont(new java.awt.Font("Tahoma", 1, 20)); // NOI18N
        directorTextField.setHorizontalAlignment(javax.swing.JTextField.CENTER);

        jLabel5.setText("Duration");

        durationTextField.setFont(new java.awt.Font("Tahoma", 1, 20)); // NOI18N
        durationTextField.setHorizontalAlignment(javax.swing.JTextField.CENTER);

        selectMovieBtn.setBackground(new java.awt.Color(255, 255, 255));
        selectMovieBtn.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/save.png"))); // NOI18N
        selectMovieBtn.setText("Select Movie");
        selectMovieBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                selectMovieBtnActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(durationTextField)
                            .addComponent(salesSystemFilmTitle)
                            .addComponent(starringTextField)
                            .addComponent(jScrollPane2)
                            .addComponent(directorTextField))
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addGap(0, 249, Short.MAX_VALUE)
                        .addComponent(selectMovieBtn)
                        .addGap(242, 242, 242))))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(salesSystemFilmTitle, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(starringTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(3, 3, 3)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(directorTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(durationTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(selectMovieBtn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {directorTextField, durationTextField, salesSystemFilmTitle, starringTextField});

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        extrasTab.addTab("Films", jPanel3);

        jPanel4.setBackground(new java.awt.Color(255, 255, 255));

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 646, Short.MAX_VALUE)
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 358, Short.MAX_VALUE)
        );

        extrasTab.addTab("Extras", jPanel4);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(extrasTab)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGap(0, 0, Short.MAX_VALUE)
                                .addComponent(retailFrameDate, javax.swing.GroupLayout.PREFERRED_SIZE, 289, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(retailFrameDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(extrasTab))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        utilities.exitToMenu();
    }//GEN-LAST:event_formWindowClosing

    private void filmListMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_filmListMouseClicked
        
        Movie m = (Movie)al.get(filmList.getSelectedIndex());
        starringTextField.setText(m.getStarring());
        synopsisTextArea.setText(m.getSynopsis());
        salesSystemFilmTitle.setText(m.getTitle());
        directorTextField.setText(m.getDirector());
        durationTextField.setText(m.getDuration());       
        synopsisTextArea.setLineWrap(true);      // used to wrap text (go to next line) when space runs out.  
                
    }//GEN-LAST:event_filmListMouseClicked

    private void selectMovieBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selectMovieBtnActionPerformed
        // when movie is selected disable all components including this button. 
        // a yes no cancel box is required as a fallback. 
        
        String message = "Are you sure you want to purchase tickets for: "+salesSystemFilmTitle.getText();
        String title = "Selling Ticket for: "+salesSystemFilmTitle.getText();
        // display the JOptionPane showConfirmDialog
        int reply = JOptionPane.showConfirmDialog(null, message, title, JOptionPane.YES_NO_OPTION);
        if (reply == JOptionPane.YES_OPTION)
        {
        filmList.setEnabled(false);
        salesSystemFilmTitle.setEnabled(false);
        starringTextField.setEnabled(false);
        synopsisTextArea.setEnabled(false);
        directorTextField.setEnabled(false);
        durationTextField.setEnabled(false);
        selectMovieBtn.setEnabled(false); 
        }
        if (reply == JOptionPane.NO_OPTION){
            //do nothing! return to previous screen
        }   

        
            
        
    }//GEN-LAST:event_selectMovieBtnActionPerformed
//    public static void main(String args[]) {
//
////        DefaultListModel retailListModel = new DefaultListModel();//list model to store titles
////        //populate retail list model with elements from admin fram list model
////        for(int i =0; i<adminFrame.lm.getSize();i++){        
////            Object o = adminFrame.lm.getElementAt(i); // get element at position i store it in o
////            retailListModel.set(i, o);                // put object o into list model retailListModel
////        }
//
//        try {
//            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
//                if ("Nimbus".equals(info.getName())) {
//                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
//                    break;
//                }
//            }
//        } catch (ClassNotFoundException ex) {
//            java.util.logging.Logger.getLogger(retailFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (InstantiationException ex) {
//            java.util.logging.Logger.getLogger(retailFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (IllegalAccessException ex) {
//            java.util.logging.Logger.getLogger(retailFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
//            java.util.logging.Logger.getLogger(retailFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        }
//        //</editor-fold>
//
//        /* Create and display the form */
//        java.awt.EventQueue.invokeLater(new Runnable() {
//
//            public void run() {
//                new retailFrame().setVisible(true);
//            }
//        });
//    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField directorTextField;
    private javax.swing.JTextField durationTextField;
    private javax.swing.JTabbedPane extrasTab;
    public static javax.swing.JList filmList;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel retailFrameDate;
    private javax.swing.JTextField salesSystemFilmTitle;
    private javax.swing.JButton selectMovieBtn;
    private javax.swing.JTextField starringTextField;
    private javax.swing.JTextArea synopsisTextArea;
    public static javax.swing.ButtonGroup timeButtonGroup;
    // End of variables declaration//GEN-END:variables

    public void setLm(DefaultListModel lm) {
        this.lm = lm;
    }
 


}